<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="名称" prop="jyCoupons">
        <el-input
          v-model="queryParams.jyCoupons"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否新人券" prop="newUserState">
        <el-select v-model="queryParams.newUserState" placeholder="请选择是否新人券" clearable size="small">
          <el-option
            v-for="dict in newUserStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券类型" prop="couponType">
        <el-select v-model="queryParams.couponType" placeholder="请选择优惠券类型" clearable size="small">
          <el-option
            v-for="dict in couponTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否固定有效期" prop="fixedTimeState">
        <el-select v-model="queryParams.fixedTimeState" placeholder="请选择是否固定有效期" clearable size="small">
          <el-option
            v-for="dict in fixedTimeStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择启用状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['coupons:coupons:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain

          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['coupons:coupons:edit']"
        >查看</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain

          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['coupons:coupons:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['coupons:coupons:export']"
          v-if="false"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="couponsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="名称" align="center" prop="couponName" />
      <el-table-column label="优惠券金额" align="center" prop="money" />
      <el-table-column label="满多少元可用" align="center" prop="minPrice" />
      <el-table-column label="是否限量" align="center" prop="limitedState">
        <template slot-scope="scope">
          <dict-tag :options="limitedStateOptions" :value="scope.row.limitedState"/>
        </template>
      </el-table-column>
      <el-table-column label="限量数" align="center" prop="total" />
      <el-table-column label="剩余数量" align="center" prop="lastTotal" />
<!--      <el-table-column label="所属门店id" align="center" prop="storeId" />-->
      <el-table-column label="是否新人券" align="center" prop="newUserState">
        <template slot-scope="scope">
          <dict-tag :options="newUserStateOptions" :value="scope.row.newUserState"/>
        </template>
      </el-table-column>
      <el-table-column label="优惠券类型" align="center" prop="couponType">
        <template slot-scope="scope">
          <dict-tag :options="couponTypeOptions" :value="scope.row.couponType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="关联的分类/商品id" align="center" prop="primaryKey" />-->
      <el-table-column label="是否固定有效期" align="center" prop="fixedTimeState">
        <template slot-scope="scope">
          <dict-tag :options="fixedTimeStateOptions" :value="scope.row.fixedTimeState"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="排序" align="center" prop="sort" />-->
      <el-table-column label="启用状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="stateOptions" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state == 'N'"
            @click="handleShelve(scope.row,'Y')"
            v-hasPermi="['coupons:coupons:edit']"
          >启用</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state == 'Y'"
            @click="handleShelve(scope.row,'N')"
            v-hasPermi="['coupons:coupons:edit']"
          >停用</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['coupons:coupons:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleDelete(scope.row)"
            v-hasPermi="['coupons:coupons:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-form-item label="名称" prop="couponName">
            <el-input v-model="form.couponName" placeholder="请输入名称" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
          <el-form-item label="优惠券金额" prop="money" >
            <el-input v-model="form.money" type="number" step="0.01" placeholder="请输入优惠券金额" />
          </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="满多少元可用" prop="minPrice">
            <el-input v-model="form.minPrice" type="number" step="0.01" placeholder="请输入满多少元可用" />
          </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
          <el-form-item label="是否限量">
            <el-radio-group v-model="form.limitedState">
              <el-radio
                v-for="dict in limitedStateOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.limitedState == 'Y'">
          <el-form-item label="限量数" prop="total">
            <el-input v-model="form.total" type="number" placeholder="请输入限量数" />
          </el-form-item>
          </el-col>
        </el-row>
      <!--  <el-form-item label="剩余数量" prop="lastTotal">
          <el-input v-model="form.lastTotal" placeholder="请输入剩余数量" />
        </el-form-item>-->
        <el-form-item label="是否新人券">
          <el-radio-group v-model="form.newUserState">
            <el-radio
              v-for="dict in newUserStateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券类型" prop="couponType">
              <el-select v-model="form.couponType" placeholder="请选择优惠券类型" @change="changeType">
                <el-option
                  v-for="dict in couponTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.couponType == 'classify'">
            <el-form-item label="分类" prop="primaryKey">
              <el-select v-model="form.primaryKey" placeholder="请选择分类" multiple>
                <el-option
                  v-for="item in classifyList"
                  :key="item.id"
                  :label="item.classifyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="可领取开始时间" prop="receiveStartTime">
              <el-date-picker clearable size="small"
                v-model="form.receiveStartTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择可领取开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可领取结束时间" prop="receiveEndTime">
              <el-date-picker clearable size="small"
                v-model="form.receiveEndTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择可领取结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否固定有效期" prop="fixedTimeState">
          <el-select v-model="form.fixedTimeState" placeholder="请选择是否固定有效期">
            <el-option
              v-for="dict in fixedTimeStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-row>
        <el-row v-if="form.fixedTimeState == 'N'">
          <el-col :span="12">
            <el-form-item label="可使用开始时间" prop="useStartTime">
              <el-date-picker clearable size="small"
                v-model="form.useStartTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择可使用开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可使用结束时间" prop="useEndTime">
              <el-date-picker clearable size="small"
                v-model="form.useEndTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择可使用结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="form.fixedTimeState == 'Y'">
          <el-col :span="12">
            <el-form-item label="有效期天数" prop="validDay">
              <el-input v-model="form.validDay" type="number" placeholder="请输入有效期天数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" type="number" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-form-item label="名称" prop="couponName">
            <el-input v-model="form.couponName" placeholder="请输入名称"disabled />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券金额" prop="money" >
              <el-input v-model="form.money" type="number" step="0.01" placeholder="请输入优惠券金额"disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="满多少元可用" prop="minPrice">
              <el-input v-model="form.minPrice" type="number" step="0.01" placeholder="请输入满多少元可用"disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否限量">
              <el-radio-group v-model="form.limitedState" disabled>
                <el-radio
                  v-for="dict in limitedStateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.limitedState == 'Y'">
            <el-form-item label="限量数" prop="total">
              <el-input v-model="form.total" type="number" placeholder="请输入限量数" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--  <el-form-item label="剩余数量" prop="lastTotal">
            <el-input v-model="form.lastTotal" placeholder="请输入剩余数量" />
          </el-form-item>-->
        <el-form-item label="是否新人券">
          <el-radio-group v-model="form.newUserState" disabled>
            <el-radio
              v-for="dict in newUserStateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券类型" prop="couponType">
              <el-select v-model="form.couponType" placeholder="请选择优惠券类型" @change="changeType" disabled>
                <el-option
                  v-for="dict in couponTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.couponType == 'classify'">
            <el-form-item label="分类" prop="primaryKey">
              <el-select v-model="form.primaryKey" placeholder="请选择分类" multiple disabled>
                <el-option
                  v-for="item in classifyList"
                  :key="item.id"
                  :label="item.classifyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="可领取开始时间" prop="receiveStartTime">
              <el-date-picker clearable size="small"
                              v-model="form.receiveStartTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择可领取开始时间"
                              disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可领取结束时间" prop="receiveEndTime">
              <el-date-picker clearable size="small"
                              v-model="form.receiveEndTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择可领取结束时间"
                              disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否固定有效期" prop="fixedTimeState">
          <el-select v-model="form.fixedTimeState" placeholder="请选择是否固定有效期"disabled>
            <el-option
              v-for="dict in fixedTimeStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-row>
        <el-row v-if="form.fixedTimeState == 'N'">
          <el-col :span="12">
            <el-form-item label="可使用开始时间" prop="useStartTime">
              <el-date-picker clearable size="small"
                              v-model="form.useStartTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择可使用开始时间"
                              disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可使用结束时间" prop="useEndTime">
              <el-date-picker clearable size="small"
                              v-model="form.useEndTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择可使用结束时间"
                              disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="form.fixedTimeState == 'Y'">
          <el-col :span="12">
            <el-form-item label="有效期天数" prop="validDay">
              <el-input v-model="form.validDay" type="number" placeholder="请输入有效期天数"disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" type="number" placeholder="请输入排序" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.state" disabled>
            <el-radio
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCoupons, getCoupons, logicDelCoupons, addCoupons, updateCoupons, exportCoupons,shelve } from "@/api/coupons/coupons";
import {listClassify} from '@/api/storeclassify/storeclassify'
import { storeListParam } from "@/api/store/store";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Coupons",
  components:{Treeselect},
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      viewOpen:false,
      // 是否限量字典
      limitedStateOptions: [],
      // 归属 平台/门店字典
      belongTypeOptions: [],
      // 是否新人券字典
      newUserStateOptions: [],
      // 优惠券类型字典
      couponTypeOptions: [],
      // 是否固定有效期字典
      fixedTimeStateOptions: [],
      // 启用状态字典
      stateOptions: [],
      //门店选择器
      storeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jyCoupons: null,
        money: null,
        minPrice: null,
        limitedState: null,
        total: null,
        lastTotal: null,
        belongType: 'platform',
        storeId: null,
        newUserState: null,
        couponType: null,
        primaryKey: null,
        receiveStartTime: null,
        receiveEndTime: null,
        fixedTimeState: null,
        useStartTime: null,
        useEndTime: null,
        validDay: null,
        sort: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        couponName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "优惠券金额不能为空", trigger: "blur" }
        ],
        minPrice: [
          { required: true, message: "满多少元可用不能为空", trigger: "blur" }
        ],
        limitedState: [
          { required: true, message: "是否限量不能为空", trigger: "blur" }
        ],
        total: [
          { required: true, message: "限量数不能为空", trigger: "blur" }
        ],
        newUserState: [
          { required: true, message: "是否新人券不能为空", trigger: "blur" }
        ],
        couponType: [
          { required: true, message: "优惠券类型不能为空", trigger: "change" }
        ],
        primaryKey: [
          { required: true, message: "关联的分类不能为空", trigger: "blur" }
        ],
        receiveStartTime: [
          { required: true, message: "可领取开始时间不能为空", trigger: "blur" }
        ],
        receiveEndTime: [
          { required: true, message: "可领取结束时间不能为空", trigger: "blur" }
        ],
        fixedTimeState: [
          { required: true, message: "是否固定有效期不能为空", trigger: "change" }
        ],
        useStartTime: [
          { required: true, message: "可使用开始时间不能为空", trigger: "blur" }
        ],
        useEndTime: [
          { required: true, message: "可使用结束时间不能为空", trigger: "blur" }
        ],
        validDay: [
          { required: true, message: "有效期天数不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "启用状态不能为空", trigger: "blur" }
        ],
      },
      classifyParams: {
        pageNum: 1,
        pageSize: 999,
      },
      classifyList:[]
    };
  },
  created() {
    this.getList();
    this.calHeight()
      this.getDicts("sys_yes_no").then(response => {
      this.limitedStateOptions = response.data;
    });
    this.getDicts("jy_belong_type").then(response => {
      this.belongTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.newUserStateOptions = response.data;
    });
    this.getDicts("jy_coupon_type").then(response => {
      let arr = []
      for(let item of response.data){
        if(item.dictValue != 'product'){
          arr.push(item)
        }
      }
      this.couponTypeOptions = arr
    });
    this.getDicts("sys_yes_no").then(response => {
      this.fixedTimeStateOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.stateOptions = response.data;
    });
    storeListParam().then(res =>{
      this.storeOptions = res
    })
    listClassify(this.classifyParams).then(res =>{
      this.classifyList = res.rows
    })
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    changeType(e){
      console.log(e)
      if(e == 'general'){
        this.form.primaryKey = null
      }
    },
    normalizer(node) {
      return {
        id: node.dictValue,
        label: node.dictLabel,
      };
    },
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询优惠券列表 */
    getList() {
      this.loading = true;
      listCoupons(this.queryParams).then(response => {
        this.couponsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.viewOpen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jyCoupons: null,
        money: null,
        minPrice: null,
        limitedState: "N",
        total: null,
        lastTotal: null,
        belongType: 'platform',
        storeId: null,
        newUserState: "N",
        couponType: 'general',
        primaryKey: null,
        receiveStartTime: null,
        receiveEndTime: null,
        fixedTimeState: 'N',
        useStartTime: null,
        useEndTime: null,
        validDay: null,
        sort: null,
        state: "Y",
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCoupons(id).then(response => {
        if(response.data.primaryKey){
          var classify = response.data.primaryKey.split(",")
          let arr = []
          for(var j = 0; j < classify.length; j++){
            for(var i = 0; i < this.classifyList.length; i++){
              if(classify[j] == this.classifyList[i].id + ''){
                arr.push(this.classifyList[i].classifyName)
              }
            }
          }
          response.data.primaryKey = arr
        }
        this.form = response.data;
        this.viewOpen = true;
        this.title = "查看优惠券";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form)
          if(this.form.couponType == 'classify'){
            var str = this.form.primaryKey.join(",")
            this.form.primaryKey = ','+str+','
          }
          if (this.form.id != null) {
            updateCoupons(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoupons(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleShelve(row,state) {
      const id = row.id
      var str = state=="Y"?'启用':'停用'
      this.$confirm('是否确认'+str+"?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return shelve(id,state);
      }).then(() => {
        this.getList();
        this.msgSuccess(str+"成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelCoupons(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优惠券数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCoupons(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
